golang+libreffice6.2实现word,excel,pptx转pdf,html

您所在的位置:网站首页 go word转pdf golang+libreffice6.2实现word,excel,pptx转pdf,html

golang+libreffice6.2实现word,excel,pptx转pdf,html

2023-12-01 17:03| 来源: 网络整理| 查看: 265

golang+libreffice6.2实现word,excel,pptx转pdf,html 我是不会赢的 · · 2272 次点击 · · 开始浏览     这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。 第一次,站长亲自招 Gopher 了>>>

golang + libreoffice6.2 实现 word,excel,pptx 转pdf/html

方法[^本地需安装libreoffice]

/** *@tips libreoffice 转换指令: * libreoffice6.2 invisible --convert-to pdf csDoc.doc --outdir /home/[转出目录] * * @function 实现文档类型转换为pdf或html * @param command:libreofficed的命令(具体以版本为准);win:soffice; linux:libreoffice6.2 * fileSrcPath:转换文件的路径 * fileOutDir:转换后文件存储目录 * converterType:转换的类型pdf/html * @return fileOutPath 转换成功生成的文件的路径 error 转换错误 */ func FuncDocs2Pdf(command string, fileSrcPath string, fileOutDir string, converterType string) (fileOutPath string, error error) { //校验fileSrcPath srcFile, erByOpenSrcFile := os.Open(fileSrcPath) if erByOpenSrcFile != nil && os.IsNotExist(erByOpenSrcFile) { return "", erByOpenSrcFile } //如文件输出目录fileOutDir不存在则自动创建 outFileDir, erByOpenFileOutDir := os.Open(fileOutDir) if erByOpenFileOutDir != nil && os.IsNotExist(erByOpenFileOutDir) { erByCreateFileOutDir := os.MkdirAll(fileOutDir, os.ModePerm) if erByCreateFileOutDir != nil { logs.Error("File ouput dir create error.....", erByCreateFileOutDir.Error()) return "", erByCreateFileOutDir } } //关闭流 defer func() { _ = srcFile.Close() _ = outFileDir.Close() }() //convert cmd := exec.Command(command, "--invisible", "--convert-to", converterType, fileSrcPath, "--outdir", fileOutDir) byteByStat, errByCmdStart := cmd.Output() //命令调用转换失败 if errByCmdStart != nil { return "", errByCmdStart } //success fileOutPath = fileOutDir + "/" + strings.Split(path.Base(fileSrcPath), ".")[0] if converterType == "html" { fileOutPath += ".html" } else { fileOutPath += ".pdf" } logs.Info("文件转换成功...", string(byteByStat)) return fileOutPath, nil }

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:我是不会赢的

查看原文:golang+libreffice6.2实现word,excel,pptx转pdf,html

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信 2272 次点击   加入收藏 微博 赞


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3